 #define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    double myPow(double x, int n) {
        return n < 0 ? 1 / (pow(x, -(long long)n)) : pow(x, n);
    }

    double pow(double x, long long n)
    {
        if (n == 0) return 1.0;
        double tmp = pow(x, n / 2);
        return n % 2 == 0 ? tmp * tmp : tmp * tmp * x;
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool evaluateTree(TreeNode* root) {
        //叶子节点，这里是满二叉树
        if (root->left == nullptr) return root->val == 1 ? true : false;

        //得到左右节点的值
        bool left = evaluateTree(root->left);
        bool right = evaluateTree(root->right);

        //判断当前节点得出值
        return root->val == 2 ? left | right : left & right;

    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int sumNumbers(TreeNode* root) {
        return dfs(root, 0);
    }

    int dfs(TreeNode* root, int presum)
    {
        //获取当前总和
        presum = presum * 10 + root->val;
        if (root->left == nullptr && root->right == nullptr) return presum;

        int ret = 0;
        if (root->left) ret += dfs(root->left, presum);
        if (root->right) ret += dfs(root->right, presum);

        return ret;
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* pruneTree(TreeNode* root) {
        if (root == nullptr) return nullptr;

        root->left = pruneTree(root->left);
        root->right = pruneTree(root->right);

        if (root->left == nullptr && root->right == nullptr && root->val == 0)
        {
            delete root;
            root = nullptr;
        }

        return root;
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    long prev = LONG_MIN;
    bool isValidBST(TreeNode* root) {
        //空节点直接返回true
        if (root == nullptr) return true;

        bool left = isValidBST(root->left);
        //剪枝
        if (left == false) return false;

        //判断当前节点的值是否满足二叉搜索数
        bool cur = false;
        if (root->val > prev) cur = true;

        prev = root->val;
        bool right = isValidBST(root->right);

        return left & cur & right;
    }
};